/*
package com.nginx.web.aspectj;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nginx.web.entity.ApiRecord;
import com.nginx.web.entity.User;
import com.nginx.web.service.ApiRecordService;
import com.nginx.web.service.impl.UserServiceImpl;
import com.nginx.web.util.ApiUtil;
import com.nginx.web.util.Result;
import com.nginx.web.util.ResultEnum;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Component
@Aspect
public class OpenApiAspect {

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private ApiRecordService apiRecordService;


//    @Around("execution(* com.nginx.web.controller.ApiController.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Exception {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        String userKey = request.getHeader("userKey");
        if (StrUtil.isEmpty(userKey)) {
            ApiUtil.back(ResultEnum.ERROR, "请求头未填写用户密钥userKey");
        }
        User user = userService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getSecretKey, userKey));
        if (user == null) {
            ApiUtil.back(ResultEnum.ERROR, "用户密钥userKey错误");
        }
        StpUtil.login(user.getId());

        Long userId = StpUtil.getLoginIdAsLong();

        String requestURI = request.getRequestURI();
        Map<String, String> parameterMap = getParameterMap(request);
        String ipAddr = getIpAddr(request);

        ApiRecord apiRecord = new ApiRecord();
        apiRecord.setUserId(userId);
        apiRecord.setName(getName(requestURI));
        apiRecord.setParam(JSONUtils.toJSONString(parameterMap));
        apiRecord.setIp(ipAddr);
        apiRecord.setUrl(requestURI);
        Object proceed;
        try {
            long start = System.currentTimeMillis();
            proceed = joinPoint.proceed();
            long end = System.currentTimeMillis();

            Result result = (Result) proceed;
            apiRecord.setResponText(JSONObject.toJSONString(result.getResult()));
            apiRecord.setIsPass(result.getCode() == 0 ? 1 : 0);
            apiRecord.setErrorTip(result.getMsg());
            apiRecord.setCallDuration(end - start);
            return proceed;

        } catch (Exception e) {
            e.printStackTrace();
            throw  e;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        } finally {
            apiRecordService.save(apiRecord);
        }
    }

    private Map<String, String> getParameterMap(HttpServletRequest request) {
        Map<String, String> parameterMap = new HashMap<>();

        if (request != null && request.getParameterNames() != null) {
            Enumeration<String> parameterNames = request.getParameterNames();
            while (parameterNames.hasMoreElements()) {
                String parameterName = parameterNames.nextElement();
                String parameterValue = request.getParameter(parameterName);
                parameterMap.put(parameterName, parameterValue);
            }
        }

        return parameterMap;
    }

    public static String getIpAddr(HttpServletRequest request) {
        System.out.println(request);
        String ipAddress;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if ("127.0.0.1".equals(ipAddress)) {
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) {
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }
        return ipAddress;
    }


    private String getName(String url) {
        switch (url){
            case "/engine/api/getcollection" : return "获取所有规则合集列表";
            case "/engine/api/getrulelist" : return "获取合集下的检查规则";
            case "/engine/api/getcollectionparam" : return "获取调用合集所需的参数";
            case "/engine/api/executecollection" : return "执行检查规则是否通过";
            default: return null;
        }
    }


}
*/
